library(ggplot2)
library(tidyr)
library(tidyverse)


# Figure 6: Cumulative Proportion of Respondents Supporting Nuclear Use 

setwd("/Users/dmin/Dropbox/Replication_Data/Paper_2/")

Dat <- read.csv("Paper_2_Data_Clean.csv", stringsAsFactors = FALSE)


Dat3 <- Dat

# This makes it so that people who never select nuclear use end up counted but don't cause the graph to spike at the end
Dat3$outcome_custom[Dat3$outcome_custom==110]<-100000

# Cumulative chart with lines -- by Theory


x =seq(0, 100, by = 0.5)


my_ecdf <- 
  . %>% 
  split(.$NuclearTheory_fac) %>% 
  map(~ecdf(.$outcome_custom)(x)) %>% 
  data.frame() %>% 
  mutate(x = x) %>% 
  pivot_longer(-x) %>% 
  mutate(
    NuclearTheory_fac = name,
    outcome_custom = value
  )


gg_df <- 
Dat3 %>% my_ecdf()
 


many_bootstraps <- 
  1:300 %>%
  map(~ sample_n(Dat3, size = n(), replace = TRUE)) %>% 
  map_df(my_ecdf, .id = "sim") %>% 
  mutate(sim_group = paste0(sim, NuclearTheory_fac))
  

g <- ggplot(gg_df,
       aes(y = outcome_custom, x = x, group = NuclearTheory_fac, color = NuclearTheory_fac)) +
  geom_line(size = 1.5) +
  geom_line(data = many_bootstraps, aes(group = sim_group), size = 0.5, alpha = 0.02) +
  ggtitle("") + 
  theme_bw() +
  scale_color_manual(values = c(
    "yellow3",
    "green3",
    "orange3",
    "red3")) +
  ylab("Cumulative Proportion") + xlab("Likelihood of Success") +
  annotate(geom = "label", x = 25, y = .15, label = "Never Ever") +
  annotate(geom = "label", x = 25, y = .26, label = "Offense/Defense") +  
  annotate(geom = "label", x = 25, y = .35, label = "Existential\nDeterrence") +
  annotate(geom = "label", x = 25, y = .61, label = "Utilitarian") +
  coord_cartesian(xlim=c(0,100)) + 
  theme(plot.title = element_text(hjust = 0.5), 
        plot.subtitle = element_text(hjust = 0.5),
        legend.position = "none")
g

ggsave("Image_6.png", plot=g, width=2000, height=2000, units=c("px"))


# Generates blank figure for theoretical predictions

# ggplot(Dat3, aes=(x=outcome_custom))+
#  ggtitle("") + theme_bw() +
#  ylab("Cumulative Proportion") + xlab("Likelihood of Success") +
#  coord_cartesian(xlim=c(0,100), ylim=c(0,1)) + theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5)) +
#  annotate(geom = "label", x = 98, y = .0, label = "Never-Ever") +
#  annotate(geom = "label", x = 50, y = .1, label = "Offense/Defense") +  
#  annotate(geom = "label", x = 90, y = .1, label = "Existential\nDeterrence") +
#  annotate(geom = "label", x = 50, y = .55, label = "Utilitarian") 


